<?php

use yii\helpers\Html;
use kartik\grid\GridView;
use yii\helpers\ArrayHelper;
use common\models\business\BusinessVariety;
use common\models\business\BusinessPig;
use common\models\business\BusinessException;
use yii\helpers\Url;
/* @var $this yii\web\View */
/* @var $searchModel backend\modules\business\models\BusinessPigSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = '仔猪财务明细';
$this->params['breadcrumbs'][] = $this->title;
?>


<section class="wrapper site-min-height">
    <!-- page start-->
    <section class="panel">
        <header class="panel-heading">
              <?= Html::a('返回','javascript:history.back(-1)'); ?>  <?= Html::encode($this->title) ?>
        </header>
        <div class="panel-body">
            <div class="adv-table editable-table ">
 


<div class="business-pig-index">



    <?php
        echo GridView::widget([
            'dataProvider'=>$dataProvider,
            'filterModel'=>$searchModel,
            'showPageSummary'=>true,
            'pjax'=>false,
            'striped'=>true,
            'hover'=>true,
            'panel'=>['type'=>'primary', 'heading'=>'仔猪财务明细'],
            'containerOptions'=>['style'=>'overflow: auto'], // only set when $responsive = false
            'headerRowOptions'=>['class'=>'kartik-sheet-style'],
            'filterRowOptions'=>['class'=>'kartik-sheet-style'],
            'columns'=>[
                [
                    'attribute'=>'no',
                    'pageSummaryOptions'=>['class'=>'text-right text-warning'],
                    'width'=>'100px',
                ],

                [
                    'class'=>'kartik\grid\ExpandRowColumn',
                    'width'=>'50px',
                    'value'=>function ($model, $key, $index, $column) {
                        return GridView::ROW_COLLAPSED;
                    },
                    'detail'=>function ($model, $key, $index, $column) {
                        return Yii::$app->controller->renderPartial('detail', ['model'=>$model]);
                    },
                    'headerOptions'=>['class'=>'kartik-sheet-style'],
                    'expandOneOnly'=>true
                ],
                [
                    'attribute'=>'batch',
                    'width'=>'100px',
                ],
                [
                    'attribute'=>'headpersonid',
                    'value' => 'person.name',
                    'pageSummaryOptions'=>['class'=>'text-right text-warning'],
                    'width'=>'100px',
                    'filterType'=>GridView::FILTER_SELECT2,
                    'filter'=>ArrayHelper::map(\common\models\business\BusinessPerson::find()->asArray()->all(),'id','name'),
                    'filterWidgetOptions'=>[
                        'pluginOptions'=>['allowClear'=>true],
                    ],
                    'filterInputOptions'=>['placeholder'=>'选择业务人'],
//                    'group'=>true,  // enable grouping
                ],
                [
                    'attribute'=>'livery',
                    'value' => 'liveryperson.name',
                    'pageSummaryOptions'=>['class'=>'text-right text-warning'],
                    'width'=>'100px',
                    'filterType'=>GridView::FILTER_SELECT2,
                    'filter'=>ArrayHelper::map(\common\models\business\BusinessFarmers::find()->asArray()->all(),'id','name'),
                    'filterWidgetOptions'=>[
                        'pluginOptions'=>['allowClear'=>true],
                    ],
                    'filterInputOptions'=>['placeholder'=>'选择代养农户'],
//                    'group'=>true,  // enable grouping
                ],
                [
                    'label' => '种类',
                    'attribute'=>'varietyid',
                    'width'=>'100px',
                    'value'=>'variety.title',
                    'filterType'=>GridView::FILTER_SELECT2,
                    'filter'=>ArrayHelper::map(BusinessVariety::find()->asArray()->all(),'id','title'),
                    'filterWidgetOptions'=>[
                        'pluginOptions'=>['allowClear'=>true],
                    ],
                    'filterInputOptions'=>['placeholder'=>'选择种类'],
//                    'group'=>true,  // enable grouping
                ],
                [
                    'attribute'=>'canlivery',
                    'width'=>'100px',
                    'value'=>function ($model, $key, $index, $widget) {
                        return BusinessPig::$canlivery[$model->canlivery];
                    },
                    'filterType'=>GridView::FILTER_SELECT2,
                    'filter'=> BusinessPig::$canlivery,
                    'filterWidgetOptions'=>[
                        'pluginOptions'=>['allowClear'=>true],
                    ],
                    'filterInputOptions'=>['placeholder'=>'选择是否可以领养']
                ],
                [
                    'attribute'=>'weight',
                    'width'=>'100px',
                    'hAlign'=>'right',
//                    'format'=>['decimal', 2],
//                    'pageSummary'=>true,
                    'pageSummaryFunc'=>GridView::F_AVG
                ],
                [
                    'attribute'=>'height',
                    'width'=>'100px',
                    'hAlign'=>'right',
//                    'format'=>['decimal', 0],
//                    'pageSummary'=>true
                ],
                [
                    'attribute'=>'planout',
                    'value' => function($model){
                        return date('Y-m-d',$model->planout);
                    },
                    'width'=>'100px',
                    'hAlign'=>'right',
                ],
                [
                    'attribute'=>'status',
                    'width'=>'100px',
                    'format' => 'raw',
                    'value'=>function ($model, $key, $index, $widget) {

                        $outdays = \common\models\sys\SysSetting::getValue('waitout');

                        if($model->status==BusinessPig::STATUS_EXCEPTION){
                            $exception = BusinessException::find()->select('reason')->where('pigid=:id',[':id'=>$model->id])->asArray()->one();
                            return Html::label(BusinessPig::$statues[$model->status], '', ['class' => 'btn btn-xs btn-danger','data-id'=>$model->id,'data-toggle'=>'tooltip','data-placement'=>'top','title'=>$exception['reason']]);
                        }else if($model->planout-time() <= $outdays*3600*24 && !empty($model->planout)){
                            return Html::label('预出栏', '', ['class' => 'btn btn-xs','style'=>'background:orange']);
                        }else if($model->status==BusinessPig::STATUS_BRFOREIN) {
                            return Html::label(BusinessPig::$statues[$model->status], '', ['class' => 'btn btn-xs btn-default']);
                        }else if($model->status==BusinessPig::STATUS_AFTERIN){
                            return Html::label(BusinessPig::$statues[$model->status], '', ['class' => 'btn btn-xs btn-primary']);
                        }else if($model->status==BusinessPig::STATUS_WAITOUT){
                            return Html::label(BusinessPig::$statues[$model->status], '', ['class' => 'btn btn-xs btn-info']);
                        }else if($model->status==BusinessPig::STATUS_KILLED){
                            return Html::label(BusinessPig::$statues[$model->status], '', ['class' => 'btn btn-xs btn-success']);
                        }

                        return Html::label(BusinessPig::$statues[$model->status], '', ['class' => 'btn btn-xs btn-default']);
                    },
                    'filterType'=>GridView::FILTER_SELECT2,
                    'filter'=> BusinessPig::$statues,
                    'filterWidgetOptions'=>[
                        'pluginOptions'=>['allowClear'=>true],
                    ],
                    'filterInputOptions'=>['placeholder'=>'选择状态']
                ],
                [
                    'class' => '\kartik\grid\ActionColumn',
                    'deleteOptions' => ['label' => '<i class="glyphicon glyphicon-remove"></i>'],
                    'options' => ['width' => '100px;'],
                    'template' => '{view}',
                    'buttons' => [
                        'view' => function ($url, $model) {
                            return Html::a('<span class="glyphicon glyphicon-eye-open"></span>',Url::to(['/business/pig/view','id'=>$model->id]),[
                                'title' => '详情'
                            ]);
                        }
                    ],
                ],
               /* [
                    'class' => '\kartik\grid\CheckboxColumn'
                ]*/
            ],
            'toolbar'=> [

                '{export}',
                '{toggleData}',
            ],
            'bordered'=>true,
            'condensed'=>true,
        ]);
    ?>



</div>
</div>
</div>
</section>
</section>
<script>
    <?php $this->beginBlock('js_end') ?>
    <?php $this->endBlock(); ?>
    <?php $this->registerJs($this->blocks['js_end'],\yii\web\View::POS_END);//将编写的js代码注册到页面底部 ?>
</script>



